<template>
<div>
  <div class="todo-container">
    <div class="todo-wrap">
     <Header :add="add"></Header>
     <List :todos="todos" :deleteId="deleteId" :changeDone="changeDone"></List>
     <Footer :deleteIdAll="deleteIdAll" :todos="todos" :changeAllDone="changeAllDone"></Footer>
    </div>
  </div>
</div>
</template>

<script>
import Header from "@/components/Header";
import List from "@/components/List";
import Footer from "@/components/Footer";
export default {
    name:"App",
    components:{
      Footer,
      List,
      Header
    },
    data(){
      return{
        todos:[]
      }
    },
    methods:{
      //添加数据
      add(e){
          const title = e.target.value.trim();
          if(title.length === 0 ){
            alert("请输入内容")
            return
          }
          if(this.todos.find(v=>v.title === title)){
            alert("请勿输入重复内容")
            return;
          }
          const todoObj = {id:Math.random().toString(36).slice(2),title,done:false}
          this.todos.unshift(todoObj);
          e.target.value = null
      },
      //更改单选
      changeDone(id){
        this.todos = this.todos.map(v=>{
          if(v.id === id){
            v.done = !v.done
          }
          return v
        })
      },
      //删除数据
      deleteId(id){
        this.todos = this.todos.filter(v=>v.id !== id)
      },
      //批量删除
      deleteIdAll(){
        this.todos = this.todos.filter(v=> !v.done )
      },
      // 批量更改选中
      changeAllDone(newVal){
        this.todos = this.todos.map(v=>{
          v.done = newVal
          return v
        })
      }
    },
    watch:{
      todos:{
        deep:true,
        handler(){
          localStorage.setItem("todos",JSON.stringify(this.todos))
        }
      }
    },
    mounted() {
      this.todos = JSON.parse(localStorage.getItem("todos") || "[]")
    }
}
</script>


<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
  0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>